package com.iu.uitl;

import java.sql.*;

public class DBUtil {
    static String dbName="note_db";
    static String name="root";
    static String password="root";
    static String url="jdbc:mysql://localhost:3306/"+dbName+"?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Hongkong&allowPublicKeyRetrieval=true";
    public static Connection  getConnection(){
        Connection connection=null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection= DriverManager.getConnection(url,name,password);

        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
            System.out.println("获取数据库连接失败");
        }
        return connection;
    }
    public static void closeResources(Connection c, Statement s,ResultSet r){
        if (r!=null){
            try {
                r.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (s!=null){
            try {
                s.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (c!=null){
            try {
                c.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

//    public static void main(String[] args) {
//        Connection connection = getConnection();
//        Statement statement;
//
//        {
//            try {
//                statement = connection.createStatement();
//                statement.execute("insert into note_user(name,pass,tel) values('zhangsan','123456','11212121212')");
//                closeResources(connection,statement,null);
//            } catch (SQLException e) {
//                e.printStackTrace();
//            }
//        }
//    }
}
